import android.app.PendingIntent
import android.content.Intent
import android.nfc.NfcAdapter
import androidx.appcompat.app.AppCompatActivity

/**
 *  package :
 *  author : zhiyi
 *  date : 25/09/2020 11:48 AM
 *  project : NfcTest
 *  description :
 */
open class BaseNfcActivity : AppCompatActivity() {
    private var mNfcAdapter: NfcAdapter? = null
    private var mPendingIntent: PendingIntent? = null

    /**
     * 启动Activity，界面可见时
     */
    override fun onStart() {
        super.onStart()
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this)
        //一旦截获NFC消息，就会通过PendingIntent调用窗口
        mPendingIntent = PendingIntent.getActivity(this, 0, Intent(this, javaClass), 0)
    }

    /**
     * 获得焦点，按钮可以点击
     */
    public override fun onResume() {
        super.onResume()
        //设置处理优于所有其他NFC的处理
        if (mNfcAdapter != null) mNfcAdapter!!.enableForegroundDispatch(
            this,
            mPendingIntent,
            null,
            null
        )
    }

    /**
     * 暂停Activity，界面获取焦点，按钮可以点击
     */
    public override fun onPause() {
        super.onPause()
        //恢复默认状态
        if (mNfcAdapter != null) mNfcAdapter!!.disableForegroundDispatch(this)
    }
}
